//                 _              
//                | |             
//   __ _ _ __ ___| |__ __  _____ 
//  / _` | '__/ __| '_ \\ \/ / __|
// | (_| | | | (__| | | |>  <\__ \
//  \__,_|_|  \___|_| |_/_/\_\___/
//                                
// Copyright 2010 Scott Dixon http://www.archxs.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package com.archxs.commons;

import org.junit.Assert;
import org.junit.Test;

import com.archxs.commons.Variant;

/**
 * Tests of the extension mechanisms available for the Variant type.
 */
public class VariantExtensionTest {
    
    @SuppressWarnings("serial")
    @Test
    public void testExtensionMechanism() {
        final Variant myVariantType = new Variant("red") {
            @Override
            protected boolean canConvertToLong(final String normalizedString) {
                if ("red".equalsIgnoreCase(normalizedString)) {
                    return true;
                } else {
                    return false;
                }
            }
            
            @Override
            protected long convertToLong(final String normalizedString) {
                if ("red".equalsIgnoreCase(normalizedString)) {
                    return 0xFF0000;
                } else {
                    return super.convertToLong(normalizedString);
                }
            }
            
            @Override
            protected boolean canConvertToDouble(final String normalizedString) {
                return canConvertToLong(normalizedString);
            }
            
            @Override
            protected double convertToDouble(final String normalizedString) {
                return (double)convertToLong(normalizedString);
            }
        };
        Assert.assertEquals(0xFF0000, myVariantType.longValue());
        Assert.assertEquals(0xFF0000, myVariantType.doubleValue(), 0x02);
    }
    
    @Test(expected=UnsupportedOperationException.class)
    public void testInvalidCallToDoubleExtension() {
        final Variant foo = Variant.valueOf("foo");
        foo.convertToDouble(foo.getNormalizedString());
    }
    
    @Test(expected=UnsupportedOperationException.class)
    public void testInvalidCallToLongExtension() {
        final Variant foo = Variant.valueOf("foo");
        foo.convertToLong(foo.getNormalizedString());
    }
}
